<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of dueltop
 *
 * @author Samuel
 */
defined('BASEPATH') or die('forbidden');

class dueltop_Controller extends Controller {

	private $inc;
	private $addwayList;	//途径
	private $platformList;	//平台列表
	private $professionList;	//职业列表
	private $logdb;	//数据库连接
	private $difflist;	//副本难度
	private $copylist;
	private $nameConn;
	private $nameList;
	
	function _init() {
		check_level(50, segment(0), segment(1), segment(2, 'index'));
		set_time_limit(0);
		$this->inc = $this->config['inc'];
		$this->platformList = $this->inc['platform_list'];
		$this->professionList = $this->inc['type_proffessionid'];
		//$this->addwayList = get_type_addway();
		//$this->copylist = get_type_copy();
		
		//得到选择的区号
		$areanum = $_SESSION['area_selected_id'];
        if ($areanum == '') 
        {
            echo "<script>alert('" . translate('C_SELECT_AREA_TEXT') . "');</script>";
            return;
        }
        $this->nameConn = getTmpDbConnect(PLATFORM_ALL, 'all', $areanum, DBTYPE_GAMESERVER);
        $this->nameConn->query("SELECT id, name FROM player");
        while ($n = $this->nameConn->get_array('default'))
        {
        	$this->nameList[$n['id']] = $n['name'];
        }
        $this->nameConn->free_all();
        
        // log数据库
		$this->logdb = getTmpDbConnect(PLATFORM_ALL, 'all', $areanum, DBTYPE_LOG);
		
		if (false == $this->logdb) {
			//内部有提示错误，在此就不提示了
			 //echo "<script>alert(' connect areanum:$areanum error ');</script>";
            return;
		}	
		$this->difflist=array('0'=>'普通','1'=>'冒险','2'=>'王者');
	}

	function index() {
		
		$this->view->display("logbattle/dueltop/index");
	}

	function flexigrid() {
		list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
		$sql = "SELECT COUNT(*) AS num FROM dueltop ";
		//$sql = "SELECT COUNT(*) AS num FROM $row[logname].accountlogin  where {$where} ";
		//echo " sql2:$sql ";
		$one = $this->logdb->one($sql);
		$total = $one['num'];
		$orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";
			
		$sql = "SELECT {$colsinfo} FROM dueltop  where {$where}  {$orderby} LIMIT {$offset},{$rp}";
		//$sql = "SELECT {$colsinfo} FROM $row[logname].accountlogin  where {$where}  {$orderby} LIMIT {$offset},{$rp}";
		//echo "sql2 = $sql";
		$this->logdb->query($sql);
		//$id = 0;
		while ($n = $this->logdb->get_array('default'))
		{
			$n['one_platform']=$this->platformList[$n['one_platform']]."(".$n['one_platform'].")";
			$n['two_platform']=$this->platformList[$n['two_platform']]."(".$n['two_platform'].")";
			
			$n['one_pro']=$this->professionList[$n['one_pro']]."(".$n['one_pro'].")";
			$n['two_pro']=$this->professionList[$n['two_pro']]."(".$n['two_pro'].")";
			$n['one_role'] = $this->nameList[$n['one_role']]."(".$n['one_role'].")";
			$n['two_role'] = $this->nameList[$n['two_role']]."(".$n['two_role'].")";
			$n['victory'] = $this->nameList[$n['victory']]."(".$n['victory'].")";
			$n['surrender'] = $this->nameList[$n['surrender']]."(".$n['surrender'].")";
			//$n['copy_difficulty']=$this->difflist[$n['copy_difficulty']]."(".$n['copy_difficulty'].")";
			//$n['copy_id']=$this->copylist[$n['copy_id']]."(".$n['copy_id'].")";
			$rows[] = array('id' => $n['id'], 'cell' => $n);
			//$id = $id+1;
		}
		//var_dump($rows);
		$result = array(
	            'page' => $page,
	            'total' => $total,
	            'rows' => $rows
		);
			
		//$this->logdb->free_all();
		echo json_encode($result);
	}

}

?>
